package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotAlarmTrigger;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotAlarmTriggerDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2021-03-04 19:17:09
 */
public class IotAlarmTriggerConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotAlarmTriggerConverts.class);

    public static List<IotAlarmTriggerDTO> convert(List<IotAlarmTrigger> iotAlarmTriggerList) {
        if (CollectionUtils.isEmpty(iotAlarmTriggerList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotAlarmTriggerDTO> iotAlarmTriggerListResult = Lists.newArrayList();
        for (IotAlarmTrigger iotAlarmTrigger : iotAlarmTriggerList) {
            iotAlarmTriggerListResult.add(convert(iotAlarmTrigger));
        }
        return iotAlarmTriggerListResult;
    }

    public static IotAlarmTriggerDTO convert(IotAlarmTrigger iotAlarmTrigger) {
        try {
            return BeanUtils.copy(iotAlarmTrigger, IotAlarmTriggerDTO.class);
        } catch (Exception e) {
            LOG.error("IotAlarmTrigger到IotAlarmTriggerDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotAlarmTrigger到IotAlarmTriggerDTO转换失败", e);
        }
    }

    public static IotAlarmTrigger convert(IotAlarmTriggerDTO iotAlarmTriggerDTO) {
        try {
            return BeanUtils.copy(iotAlarmTriggerDTO, IotAlarmTrigger.class);
        } catch (Exception e) {
            LOG.error("IotAlarmTriggerDTO到IotAlarmTrigger转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotAlarmTriggerDTO到IotAlarmTrigger转换失败", e);
        }
    }
}